package com.fengshang.wedding.domain.service.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import com.fengshang.wedding.domain.model.api.IMessage;
import com.fengshang.wedding.domain.model.api.Message;
import com.fengshang.wedding.domain.model.internal.MessageEntity;
import com.fengshang.wedding.domain.service.api.IMessageManager;
import com.fengshang.wedding.domain.service.api.InvalidTopicException;
import com.fengshang.wedding.domain.service.api.MessageNotFoundException;
import com.fengshang.wedding.utils.DateFormatterHelper;
/**
 * 
 *@author gembin@gmail.com
 * 
 */
public class MessageManager implements IMessageManager {
	
	private IMessagePersistenceService messagePersistenceService;
	
	public void setMessagePersistenceService(
			IMessagePersistenceService messagePersistenceService) {
		this.messagePersistenceService = messagePersistenceService;
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IMessageManager#createMessage(int, com.fengshang.wedding.domain.model.api.IMessage)
	 */
	public void createMessage(int topicId, IMessage message)throws InvalidTopicException {
		((Message) message).setPostDate(DateFormatterHelper.format(new Date()));
		MessageEntity messageEntity=new MessageEntity(); 
		messageEntity.importData(message);
		try{
			messagePersistenceService.createMessage(topicId, messageEntity);
		}catch(EntityNotFoundException e){
			throw new InvalidTopicException("a message should belong to an existing topic!!",e);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IMessageManager#findTopicMessagesByPagination(int, int, int)
	 */
	public Collection<IMessage> findTopicMessagesByPagination(int topicId,
			int pageSize, int currentPage) {
		Collection<MessageEntity> messagesEntity=messagePersistenceService.findTopicMessagesByPagination(topicId, pageSize, currentPage);
		Collection<IMessage> messagesVo=new ArrayList<IMessage>();
		entityToValueObject(messagesEntity,messagesVo);
		return messagesVo;  
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IMessageManager#findMessagesByPagination(java.lang.String, int, int)
	 */
	public Collection<IMessage> findMessagesByPagination(String query,
			int pageSize, int currentPage) {
		Collection<MessageEntity> messagesEntity=messagePersistenceService.findMessagesByPagination(query, pageSize, currentPage);
		Collection<IMessage> messagesVo=new ArrayList<IMessage>();
		entityToValueObject(messagesEntity,messagesVo);
		return messagesVo;  
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IMessageManager#removeMessage(int)
	 */
	public void removeMessage(int messageId)throws MessageNotFoundException{
		try{
			messagePersistenceService.removeMessage(messageId);
		}catch (EntityNotFoundException e){
			throw new MessageNotFoundException(e);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IMessageManager#updateMessage(com.fengshang.wedding.domain.model.api.IMessage)
	 */
	public IMessage updateMessage(IMessage message)throws MessageNotFoundException{
		if(message==null)return null;
		int messageId=message.getId();
		MessageEntity messageEntity=messagePersistenceService.findMessageById(messageId);
		if(messageEntity==null)
			 throw new MessageNotFoundException("Message with id="+messageId+" dose not exists!");
		messageEntity.importData(message);
		return messagePersistenceService.updateMessage(messageEntity).getValueObject();
	}
	 
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IMessageManager#getTotalPages(int)
	 */
	public int getTotalPages(int pageSize) {
		return messagePersistenceService.getTotalPages(pageSize);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.api.IMessageManager#getTotalPages()
	 */
	public int getTotalPages() {
		return messagePersistenceService.getTotalPages();
	}
	
	/**
	 * @param messagesEntity
	 * @param messagesVo
	 */
	private static void entityToValueObject(final Collection<MessageEntity> messagesEntity,final Collection<IMessage> messagesVo){
		for(Iterator<MessageEntity> it=messagesEntity.iterator();it.hasNext();){
			Message message=(Message) it.next().getValueObject();
			Date postDate=message.getPostDate();
			message.setPostDate(DateFormatterHelper.format(postDate));
			messagesVo.add(message);
		}
	}

}
